package test;

import javax.microedition.lcdui.Image;
import org.xmap.ui.View;
import org.xmap.ui.ViewManager;
import org.xmap.ui.base.Window;
import org.xmap.ui.control.SimpleMenu;

public class SimpleMenuView extends View {

    private SimpleMenu bm;
    private String[] menuImageUrlNormal = new String[]{
        "start_normal.png", "resume_normal.png",
        "setup_normal.png", "return_normal.png"
    };
    private String[] menuImageUrlHover = new String[]{
        "start_hover.png", "resume_hover.png",
        "setup_hover.png", "return_hover.png"
    };

    public SimpleMenuView(View back) {
        super(back);
//        this.setBgColor(0x00ffff88);
        this.setTitleText("简单菜单控件演示");
        this.bm = new SimpleMenu(0, 0, 100, 120, 30, Window.ORIENT_LEFT_UP);
        this.addMenu("开始", 0);
        this.addMenu("继续", 1);
        this.addMenu("设置", 2);
        this.addMenu("返回", 3);
        this.bm.setListener(this);
        this.bm.setAlignX(50);
        this.bm.setAlignY(50);
        this.addControls(bm);
    }

    private void addMenu(String name, int index) {
        try {
            Image i1 = Image.createImage("".getClass().getResourceAsStream("/xmapres/simplemenu/" + this.menuImageUrlNormal[index]));
            Image i2 = Image.createImage("".getClass().getResourceAsStream("/xmapres/simplemenu/" + this.menuImageUrlHover[index]));
            this.bm.addMenu(name, i1, i2);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        Image i1 = Image.createImage(100, 40);
//        Graphics g1 = i1.getGraphics();
//        g1.setColor(0xff00ff00);
//        g1.drawString(name, 0, 0, Graphics.LEFT | Graphics.TOP);
//
//        Image i2 = Image.createImage(100, 40);
//        Graphics g2 = i2.getGraphics();
//        g2.setColor(0xffff0000);
//        g2.drawString(name, 0, 0, Graphics.LEFT | Graphics.TOP);
//
//        this.bm.addMenu(name, i1, i2);
    }
//    public void timeInterrupted() {
//        this.bm.setLineColor((int) System.currentTimeMillis());
//        this.bm.setTextColor((int) System.currentTimeMillis() + 0xffff0000);
//    }

    public void commandEvent(String command, Window sender) {
//        if (command.equals("边框显示")) {
//            Image i1 = Image.createImage(100, 40);
//            Graphics g1 = i1.getGraphics();
//            g1.setColor(0xff00ff00);
//            g1.drawString("边框隐藏", 0, 0, Graphics.LEFT | Graphics.TOP);
//            Image i2 = Image.createImage(100, 40);
//            Graphics g2 = i2.getGraphics();
//            g2.setColor(0xffff0000);
//            g2.drawString("边框隐藏", 0, 0, Graphics.LEFT | Graphics.TOP);
//            this.bm.changeMenu("边框显示", "边框隐藏", i1, i2);
//            this.bm.setDrawRect(true);
//        } else if (command.equals("边框隐藏")) {
//            Image i1 = Image.createImage(100, 40);
//            Graphics g1 = i1.getGraphics();
//            g1.setColor(0xff00ff00);
//            g1.drawString("边框显示", 0, 0, Graphics.LEFT | Graphics.TOP);
//            Image i2 = Image.createImage(100, 40);
//            Graphics g2 = i2.getGraphics();
//            g2.setColor(0xffff0000);
//            g2.drawString("边框显示", 0, 0, Graphics.LEFT | Graphics.TOP);
//            this.bm.changeMenu("边框隐藏", "边框显示", i1, i2);
//            this.bm.setDrawRect(false);
//        } else if (command.equals("背景透明")) {
//            this.bm.changeMenu("背景透明", "背景可见", null, null);
//            this.bm.setTransparent(true);
//        } else if (command.equals("背景可见")) {
//            this.bm.changeMenu("背景可见", "背景透明", null, null);
//            this.bm.setTransparent(false);
//        } else if (command.equals("调整屏幕")) {
//            Env.screenWidth = (int) (Math.abs(System.currentTimeMillis()) % ViewManager.instance.getWidth());
//            Env.screenHeight = (int) (Math.abs(System.currentTimeMillis()) % ViewManager.instance.getHeight());
        if (command.equals("返回")) {
            ViewManager.instance.setView(this.back, View.ANIMATION_LEFT_TO_RIGHT);
        } else {
            this.setTitleText("选中:" + command);
        }
    }
}
